dnl Process this file with autoconf to produce a configure script.

dnl Autoconf initialization
AC_PREREQ([2.71])
AC_INIT([zvbi],[0.2.43])

AC_CONFIG_SRCDIR(src/vbi.c)
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIRS([m4])
AC_CONFIG_AUX_DIR(build-aux)

dnl Automake initialization
AM_INIT_AUTOMAKE([1.16 check-news dist-bzip2])

dnl Store the host string without "unknown" for cross-compile use
host_str=$host

ACLOCAL_AMFLAGS="--install -I m4"
AM_MAINTAINER_MODE

dnl [current:revision:age]
dnl Any change: ++revision
dnl Interface added: ++current, revision = 0
dnl age = last binary compatible version - current
AC_SUBST(LIBZVBI_SO_VERSION, [13:2:13])

dnl Checks for programs.
dnl Partially generated by Autoscan.
AC_PROG_CC
AC_PROG_MAKE_SET
dnl For header tests only:
AC_PROG_CXX
AC_PROG_CPP


dnl math.h functions used in io-sim.c, must link -lm. The default
dnl 'action if found' is to add "-lm" to $LIBS, so leave that
dnl argument blank. Suggested by autoscan as an alternative to
dnl manipulating the LIBS variable directly.
dnl 	LIBS="$LIBS -lm"
AC_CHECK_LIB([m], [cos],,[
      AC_MSG_ERROR([Unable to link math functions])
])


dnl Checks for header files.
dnl Generated by Autoscan.
AC_PATH_X
AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h langinfo.h libintl.h
		  malloc.h netdb.h netinet/in.h stdint.h strings.h
		  sys/ioctl.h sys/mman.h sys/param.h sys/socket.h sys/statvfs.h
		  sys/time.h syslog.h unistd.h])

dnl Checks for typedefs, structures, and compiler characteristics.
dnl Generated by Autoscan.
AC_CHECK_HEADER_STDBOOL
AC_TYPE_UID_T
AC_C_INLINE
AC_TYPE_INT16_T
AC_TYPE_INT32_T
AC_TYPE_INT64_T
AC_TYPE_INT8_T
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_CHECK_MEMBERS([struct stat.st_rdev])
AC_TYPE_UINT16_T
AC_TYPE_UINT32_T
AC_TYPE_UINT64_T
AC_TYPE_UINT8_T

dnl Check to see if we need to add -lws2_32 on Windows.
dnl needed for select().
AC_CHECK_HEADER([winsock2.h], [
  AC_CHECK_HEADERS([winsock2.h])
  LIBS="$LIBS -lws2_32"
])

dnl Check for BSD/GNU extensions and optional functions.
dnl If not present we use replacements.
dnl
dnl This list is generated by autoscan, except for:
dnl - getaddrinfo: HAVE_GETADDRINFO used in "src/proxy-msg.c"
dnl   and we don't want to refactor that yet.
dnl - strptime: HAVE_STRPTIME used below for other parts of
dnl   configure.ac
AC_CHECK_FUNCS([alarm dup2 gethostbyaddr gethostbyname getopt_long gettimeofday
		inet_ntoa localtime_r memmove memset mkdir modf munmap
		nl_langinfo putenv select setenv setlocale socket strcasecmp
		strchr strdup strerror strncasecmp strndup strrchr strstr
		strtol strtoul tzset strptime getaddrinfo ffs __builtin_ffs sincos])

AM_CONDITIONAL(HAVE_STRPTIME, [test "x$HAVE_STRPTIME" = xyes])

dnl Additional checks for library functions.
dnl Generated by Autoscan.
AC_FUNC_CHOWN
AC_FUNC_ERROR_AT_LINE
AC_FUNC_FORK
AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
AC_HEADER_MAJOR
AC_FUNC_MALLOC
AC_FUNC_MKTIME
AC_FUNC_MMAP
AC_FUNC_REALLOC
AC_FUNC_STRTOD

AC_SYS_LARGEFILE

dnl AC_*_IFELSE and similar macros below here are custom checks for features that
dnl do not have a specific macro provided by autoconf. Upgrading these to
dnl autoconf 2.71 required additional quotations and usages of "AC_LANG_SOURCE".
dnl "Noteworthy changes in autoconf version 2.66 through 2.68"
dnl <https://autotools.info/forwardporting/autoconf.html>

dnl log2() is a GNU extension (a macro, not a function).
dnl If not present we use a replacement.
AC_MSG_CHECKING([for log2])
AC_LINK_IFELSE([AC_LANG_SOURCE([
#include <stdio.h>
#include <math.h>
int main (void) {
double x;
scanf ("%f", &x);
printf ("%f", log2 (x));
return 0;
}
])],[
  AC_MSG_RESULT([yes])
  AC_DEFINE(HAVE_LOG2, 1, [Define if the log2() function is available])
],[
  AC_MSG_RESULT([no])
])

dnl strerror() is not thread safe and there are different versions
dnl of strerror_r(). If none of them are present we use a replacement.
AC_MSG_CHECKING([for strerror_r])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
#include <stdlib.h>
#include <string.h>
int main (void) {
return *strerror_r (22, malloc (128), 128);
}
])],[
  AC_MSG_RESULT([yes, GNU version])
  AC_DEFINE(HAVE_GNU_STRERROR_R, 1, [Define to 1 if you have
	    the GNU version of the strerror_r() function.])
],[
  AC_COMPILE_IFELSE([AC_LANG_SOURCE([
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
printf ("%f", 1.0 + strerror_r (22, malloc (128), 128));
return 0;
}
])],[
    AC_MSG_RESULT([yes, SUSV3 version])
    AC_DEFINE(HAVE_SUSV3_STRERROR_R, 1, [Define to 1 if you have
	      the SUSV3 version of the strerror_r() function.])
  ],[
    AC_MSG_RESULT([no])
  ])
])

dnl __BYTE_ORDER is not portable.
AC_DEFINE(Z_LITTLE_ENDIAN, 1234, [naidne elttiL])
AC_DEFINE(Z_BIG_ENDIAN, 4321, [Big endian])
AC_C_BIGENDIAN(
  AC_DEFINE(Z_BYTE_ORDER, 4321, [Byte order]),
  AC_DEFINE(Z_BYTE_ORDER, 1234, [Byte order]))


dnl Initialize libtool
dnl ("LT_INIT" replaces "AC_PROG_LIBTOOL")
LT_INIT

dnl Generate "site_def.h"
test -e site_def.h || cat <<EOF >site_def.h
/* Site specific definitions */

#ifndef SITE_DEF_H
#define SITE_DEF_H
/* #define BIT_SLICER_LOG 1 */
/* #define CACHE_DEBUG 1 */
/* #define CACHE_DEBUG 2 */
/* #define CACHE_STATUS 1 */
/* #define CACHE_CONSISTENCY 1 */
/* #define DVB_DEMUX_LOG 1 */
/* #define DVB_MUX_LOG 1 */
/* #define RAW_DECODER_LOG 1 */
/* #define RAW_DECODER_PATTERN_DUMP 1 */
/* #define TELETEXT_DEBUG 1 */
#endif /* SITE_DEF_H */
EOF


dnl option, define/conditional name
AC_DEFUN([CHECK_CC_OPTION], [
  AC_MSG_CHECKING([if $CC supports $1])
  SAVE_CFLAGS="$CFLAGS"
  CFLAGS="$CFLAGS $1"
  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return 0;])],
		    [$2=yes], [$2=no])
  CFLAGS="$SAVE_CFLAGS"
  AC_MSG_RESULT($$2)
  AM_CONDITIONAL($2, [test "x$$2" = "xyes"])])

dnl option, define/conditional name
AC_DEFUN([CHECK_CXX_OPTION], [
  AC_MSG_CHECKING([if $CXX supports $1])
  SAVE_CXXFLAGS="$CXXFLAGS"
  CXXFLAGS="$CXXFLAGS $1"
  AC_LANG_PUSH(C++)
  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return 0;])],
		    [$2=yes], [$2=no])
  AC_LANG_POP()
  CXXFLAGS="$SAVE_CXXFLAGS"
  AC_MSG_RESULT($$2)
  AM_CONDITIONAL($2, [test "x$$2" = "xyes"])])

dnl See if we can / have to increase inlining limits.
CHECK_CC_OPTION([--param inline-unit-growth=3000], HAVE_GCC_LIMITS)

dnl For make check.
CHECK_CC_OPTION([-std=c89], HAVE_GCC_C89_SUPPORT)
CHECK_CC_OPTION([-std=iso9899:199409], HAVE_GCC_C94_SUPPORT)
CHECK_CC_OPTION([-std=c99], HAVE_GCC_C99_SUPPORT)
CHECK_CXX_OPTION([-std=c++98], HAVE_GXX_CXX98_SUPPORT)

dnl
dnl Check how to link pthreads functions.
dnl (-lpthread on Linux, -lpthreadGC2 [from the pthreads-win32.
dnl  package] on MinGW, -pthread on FreeBSD).
dnl
dnl When upgrading to autoconf 2.71, AC_TRY_LINK deprecated and replaced
dnl with AC_LINK_IFELSE.
dnl
AC_CHECK_LIB(pthread, pthread_create,,[
  AC_CHECK_LIB(pthreadGC2, pthread_create,,[
    AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[pthread_create();]])],[],[
      # same as previous, but use '-pthread' instead of '-lpthread'
      LDFLAGS="$LDFLAGS -pthread"
      AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[pthread_create();]])],[],[
	AC_MSG_ERROR([Unable to link pthread functions])
      ])
    ])
  ])
])

dnl
dnl See if struct tm has tm_gmtoff, a BSD/GNU extension.
dnl (Used in test/date.)
dnl
AC_MSG_CHECKING([if struct tm has tm_gmtoff])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
#include <time.h>
int main (void) {
struct tm tm;
tm.tm_gmtoff = 0;
return 0;
}
])],[
  AC_MSG_RESULT([yes])
  AC_DEFINE(HAVE_TM_GMTOFF, 1, [Define if struct tm has a tm_gmtoff field])
],[
  AC_MSG_RESULT([no])
])

dnl
dnl Check for Gnome unicode library or libc 2.1.
dnl (Teletext URE search wchar_t ctype.h functions)
dnl
AC_MSG_CHECKING(whether we are using the GNU C Library 2.1 or newer)
AC_EGREP_CPP([GLIBC21],[
#include <features.h>
#ifdef __GNU_LIBRARY__
 #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1) || (__GLIBC__ > 2)
  GLIBC21
 #endif
#endif
],[
  AC_MSG_RESULT([yes])
  AC_DEFINE(HAVE_GLIBC21, 1, [Honk if you have GNU C lib 2.1+])
],[
  AC_MSG_RESULT([no])
  AC_MSG_CHECKING(for unicode library)
  UNICODE_VERSION=`unicode-config --version`
  if test $? -eq 0; then
    AC_DEFINE(HAVE_LIBUNICODE, 1, [Define if you have libunicode])
    AC_MSG_RESULT($UNICODE_VERSION)
    UNICODE_CFLAGS=`unicode-config --cflags`
    UNICODE_LIBS=`unicode-config --libs`
    AC_SUBST(UNICODE_CFLAGS)
    AC_SUBST(UNICODE_LIBS)
  else
    AC_MSG_RESULT([not present - Teletext search disabled])
  fi
])

dnl
dnl Check for iconv() in libc or libiconv.
dnl (Unicode conversions)
dnl
dnl Using m4/iconv.m4 from the gettext package.
dnl
AM_ICONV_LINK
if test "x$am_cv_func_iconv" != xyes; then
  AC_MSG_ERROR([iconv() not found])
fi
LIBS="$LIBS $LIBICONV"

dnl
dnl Check for png library.
dnl (PNG page export)
dnl
HAVE_PNG="yes"
AC_CHECK_LIB(png, png_destroy_write_struct,
  LIBS="$LIBS -lpng -lz", HAVE_PNG="no", -lz -lm)
if test "x$HAVE_PNG" = xyes; then
  AC_DEFINE(HAVE_LIBPNG, 1, [Define if you have libpng])
fi

dnl
dnl X libraries.
dnl (Test programs)
dnl
if test "x$build" != "x$host" ; then
  # AC_PATH_XTRA does not work when cross compiling.
  if test "x$with_x" = xno || test "x$no_x" = xyes ; then
    # X_DISPLAY_MISSING is normally set by AC_PATH_XTRA
    # Set X_DISPLAY_MISSING to 1 if X is disabled or not found
    no_x=yes
    AC_DEFINE(X_DISPLAY_MISSING, 1)
  else
    # If no --x-includes or --x-libraries, add expected path
    if test "x$x_includes" != "x" ; then
      X_CFLAGS="$X_CFLAGS -I$x_includes"
    else
      X_CFLAGS="$X_CFLAGS -I/usr/include"
    fi
    
    if test "x$x_libraries" != "x" ; then
      X_LIBS="$X_LIBS -L$x_libraries"
    else
      X_LIBS="$X_LIBS -L/usr/lib/${host_str}"
    fi

    # Add includedir/libdir if they exist and were not already added above
    if test "x$includedir" != "x" && test "x$includedir" != "x/usr/include" ; then
      X_CFLAGS="$X_CFLAGS -I${includedir}"
    fi
    
    if test "x$libdir" != "x" && test "x$libdir" != "x/usr/lib/${host_str}" ; then
      X_LIBS="$X_LIBS -L${libdir}"
    fi
    
    SAVE_CPPFLAGS="$CPPFLAGS"
    SAVE_LDFLAGS="$LDFLAGS"
    CPPFLAGS="$CPPFLAGS $X_CFLAGS"
    LDFLAGS="$LDFLAGS $X_LIBS"
    AC_CHECK_HEADER(X11/Xlib.h,, no_x=yes)
    AC_CHECK_LIB(X11, XOpenDisplay,, no_x=yes)
    CPPFLAGS="$SAVE_CPPFLAGS"
    LDFLAGS="$SAVE_LDFLAGS"
  fi
else
  AC_PATH_XTRA
fi
if test "x$no_x" = xyes ; then
  X_CFLAGS=""
  X_LIBS=""
else
  X_LIBS="$X_LIBS -lX11"
fi
AM_CONDITIONAL(HAVE_X, [test "x$no_x" != xyes])

dnl
dnl Enable OS dependent device interfaces.
dnl (Linux videodev.h, videodev2.h, dvb, bktr dependencies)
dnl
enable_v4l_auto=no
enable_dvb_auto=no
enable_bktr_auto=no
enable_proxy_auto=no
run_check_scripts=no
host_windows=no

case "$host_os" in
  linux*)
    enable_v4l_auto=yes
    enable_dvb_auto=yes
    enable_proxy_auto=yes
    run_check_scripts=yes
    ;;
  freebsd* | kfreebsd*-gnu | openbsd* | netbsd*)
    enable_bktr_auto=yes
    run_check_scripts=yes
    ;;
  cygwin*|mingw*)
    host_windows=yes
    ;;
  *)
    ;;
esac
AM_CONDITIONAL([WINDOWS], [test "$host_windows" = "yes"])

build_linux_for_windows=no

if test "$host_windows" = "yes"; then
case "$build_os" in
  linux*)
    build_linux_for_windows=yes
    ;;
  *)
    ;;
esac
fi
AM_CONDITIONAL([LINUX_FOR_WINDOWS], [test "$build_linux_for_windows" = "yes"])

AC_MSG_CHECKING([whether to build the Video4Linux interface])
AC_ARG_ENABLE(v4l,
  AS_HELP_STRING([--enable-v4l],[Include the V4L and V4L2 interface (auto)]),,
  enable_v4l=$enable_v4l_auto)
AC_MSG_RESULT($enable_v4l)
if test "x$enable_v4l" = xyes; then
  AC_DEFINE(ENABLE_V4L, 1, [Define to build V4L interface])
  AC_DEFINE(ENABLE_V4L2, 1, [Define to build V4L2 / V4L2 2.5 interface])
else
  # The proxy allows sharing of a V4L/V4L2 device and will be
  # useless without the V4L/V4L2 capture functions if the library.
  enable_proxy_auto=no
fi
AM_CONDITIONAL(ENABLE_V4L, [test "x$enable_v4l" = xyes])

AC_MSG_CHECKING([whether to build the Linux DVB interface])
AC_ARG_ENABLE(dvb,
  AS_HELP_STRING([--enable-dvb],[Include the DVB interface (auto)]),,
  enable_dvb=$enable_dvb_auto)
AC_MSG_RESULT($enable_dvb)
if test "x$enable_dvb" = xyes; then
  AC_DEFINE(ENABLE_DVB, 1, [Define to build DVB interface])
fi
AM_CONDITIONAL(ENABLE_DVB, [test "x$enable_dvb" = xyes])

AC_MSG_CHECKING([whether to build the *BSD bktr driver interface])
AC_ARG_ENABLE(bktr,
  AS_HELP_STRING([--enable-bktr],[Include the *BSD bktr driver interface (auto)]),,
  enable_bktr=$enable_bktr_auto)
AC_MSG_RESULT($enable_bktr)
if test "x$enable_bktr" = xyes; then
  AC_DEFINE(ENABLE_BKTR, 1, [Define to build bktr driver interface])
fi


if test "x$enable_v4l" = xyes -o "x$enable_dvb" = xyes; then
  # Linux 2.6.x asm/types.h defines __s64 and __u64 only
  # if __GNUC__ is defined. These types are required to compile
  # videodev2.h and the Linux DVB headers.
  AC_MSG_CHECKING([if asm/types.h defines __s64 and __u64])
  AC_COMPILE_IFELSE([AC_LANG_SOURCE([#include <asm/types.h>
__s64 a = 1;
__u64 b = 2;
  ])], [AC_DEFINE(HAVE_S64_U64, 1,
		[Define if asm/types.h defines __s64 and __u64])
      AC_MSG_RESULT(yes)],
  [AC_MSG_RESULT(no)])
fi

dnl
dnl Enable vbi proxy
dnl
AC_MSG_CHECKING([whether to build the vbi proxy daemon and interface])
AC_ARG_ENABLE(proxy,
  AS_HELP_STRING([--enable-proxy],[Build the vbi proxy daemon and interface (auto)]),,
  enable_proxy=$enable_proxy_auto)
AC_MSG_RESULT($enable_proxy)
if test "x$enable_proxy" = xyes; then
  AC_DEFINE(ENABLE_PROXY, 1, [Define to build proxy daemon and interface])
  case "$host_os" in
    *-musl*)
      AC_DEFINE(HAVE_IOCTL_INT_INT_DOTS, 1, [ioctl request type])
      ;;
    linux*)
      AC_DEFINE(HAVE_IOCTL_INT_ULONG_DOTS, 1, [ioctl request type])
      ;;
    freebsd* | kfreebsd*-gnu | openbsd* | netbsd*)
      AC_DEFINE(HAVE_IOCTL_INT_ULONG_DOTS, 1, [ioctl request type])
      ;;
    *)
      ;;
  esac
fi
AM_CONDITIONAL(ENABLE_PROXY, [test "x$enable_proxy" = xyes])

AM_CONDITIONAL(RUN_CHECK_SCRIPTS, [test "x$run_check_scripts" = xyes])

dnl
dnl Native language support.
dnl
AM_GNU_GETTEXT_VERSION([0.21])
AM_GNU_GETTEXT([external], [need-ngettext])
LIBS="$LTLIBINTL $LIBS"
if test "x${prefix}" = xNONE; then
  AC_DEFINE_UNQUOTED(PACKAGE_LOCALE_DIR,
    "${ac_default_prefix}/share/locale", [ld])
else
  AC_DEFINE_UNQUOTED(PACKAGE_LOCALE_DIR,
    "${prefix}/share/locale", [ld])
fi

dnl
dnl Build docs from the sources if Doxygen is installed.
dnl
AC_ARG_WITH([doxygen],
  AS_HELP_STRING([--without-doxygen],
		 [Disable building of API documentation]),,
  [with_doxygen=yes])
if test "x$with_doxygen" = "xyes"; then
  AC_CHECK_PROG(HAVE_DOXYGEN, doxygen, yes, no)
else
  HAVE_DOXYGEN=no
fi
AM_CONDITIONAL(HAVE_DOXYGEN, [test "x$HAVE_DOXYGEN" = xyes])

dnl Helps debugging, see test/Makefile.am.
AM_CONDITIONAL(BUILD_STATIC_LIB, [test "x$enable_static" = xyes])

AC_ARG_ENABLE([tests],
  AS_HELP_STRING([--disable-tests],
     [Disable building of tests programs]),,
  [enable_tests=yes])
AM_CONDITIONAL(ENABLE_TESTS, [test "x$enable_tests" = xyes])

AC_ARG_ENABLE([examples],
  AS_HELP_STRING([--disable-examples],
     [Disable building of example programs]),,
  [enable_examples=yes])
AM_CONDITIONAL(ENABLE_EXAMPLES, [test "x$enable_examples" = xyes])

AC_CONFIG_FILES([
  Makefile
  contrib/Makefile
  examples/Makefile
  daemon/Makefile
  daemon/zvbid.init
  doc/Doxyfile
  doc/Makefile
  m4/Makefile
  src/Makefile
  src/dvb/Makefile
  test/Makefile
  po/Makefile.in
  zvbi.spec
  zvbi-0.2.pc
])

AC_OUTPUT
